﻿CREATE FUNCTION dbo.getInvSumByDate
	(
	@PUId int,
	@InvDate date
	)
RETURNS money
AS
	BEGIN

	DECLARE @invSum money

	SELECT @invSum = SUM(dbo.INVOICE_OUT_ITEM.SUM) from (dbo.INVOICE_OUT_ITEM INNER JOIN dbo.INVOICE_OUT ON dbo.INVOICE_OUT_ITEM.INVOICE_OUT_ID = dbo.INVOICE_OUT.ID) 
	WHERE dbo.INVOICE_OUT_ITEM.PRODUCT_UNIT_ID=@PUId  AND dbo.INVOICE_OUT.DATE < @InvDate 
	
	RETURN @invSum
	END